package com.yupi.springbootinit.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.yupi.springbootinit.model.entity.ExcelData;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Data
public class ExcelListener extends AnalysisEventListener<Map<String,Object>> {

    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    /**
     * 缓存的数据
     */
    private List<Map<String,Object>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    /**
     * 表头数据
     * @param headMap
     * @param context
     */
    private Map map = new HashMap<>();

    @Override
    public void invokeHeadMap(Map headMap, AnalysisContext context) {
        log.info("解析到的表头数据: {}", headMap);
        map = headMap;
    }





    @Override
    public void invoke(Map<String, Object> excelData, AnalysisContext analysisContext) {
        log.info("解析到了数据：{}",excelData);
        cachedDataList.add(excelData);
    }

    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

        log.info("所有数据解析完成！");
    }
}